package com.gitee.feizns.quickstart.log.spring.boot.autoconfigure.db;

import com.gitee.feizns.quickstart.web.WebUtils;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * 蹼
 * @author feizns
 * @since 2025/3/8
 */
public class Web {

    /**
     * 头
     * @return {@link Map}<{@link String}, {@link String}>
     */
    public static Map<String, String> headers() {
        HttpServletRequest req = WebUtils.getReq();
        Map<String, String> headers = new HashMap<>();
        Enumeration<String> enumeration = req.getHeaderNames();
        while ( enumeration.hasMoreElements() ) {
            String name = enumeration.nextElement();
            headers.put(name, req.getHeader(name));
        }
        return headers;
    }

    /**
     * 请求 URL
     * @return {@link String}
     */
    public static String requestUrl() {
        HttpServletRequest req = WebUtils.getReq();
        String queryString = req.getQueryString();
        queryString = StringUtils.hasText(queryString) ? "?" + queryString : "";
        return WebUtils.getReq().getRequestURL() + queryString;
    }

    /**
     * 请求方法
     * @return {@link String }
     */
    public static String requestMethod() {
        return WebUtils.getReq().getMethod();
    }

}
